package org.poseidon.report.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.poseidon.common.exception.ServiceException;
import org.poseidon.common.utils.DateUtils;
import org.poseidon.common.utils.SecurityUtils;
import org.poseidon.report.domain.BuMilestone;
import org.poseidon.report.domain.vo.BuProjectAddVO;
import org.poseidon.report.domain.vo.BuProjectEditVO;
import org.poseidon.report.mapper.BuDiaryMapper;
import org.poseidon.report.service.IBuDiaryService;
import org.poseidon.report.service.IBuMilestoneService;
import org.poseidon.report.service.IBuProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.poseidon.report.mapper.BuProjectMapper;
import org.poseidon.report.domain.BuProject;
import org.poseidon.report.service.IBuProjectService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 项目Service业务层处理
 * 
 * @author gfc
 * @date 2022-02-23
 */
@Service
public class BuProjectServiceImpl implements IBuProjectService 
{
    @Autowired
    private BuProjectMapper buProjectMapper;
    @Autowired
    private IBuDiaryService buDiaryService;
    @Autowired
    private IBuMilestoneService buMilestoneService;
    @Autowired
    private BuDiaryMapper buDiaryMapper;
    /**
     * 查询项目
     * 
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public BuProject selectBuProjectById(Long id)
    {
        return buProjectMapper.selectBuProjectById(id);
    }

    /**
     * 查询项目列表
     * 
     * @param buProject 项目
     * @return 项目
     */
    @Override
    public List<BuProject> selectBuProjectList(BuProject buProject)
    {
        return buProjectMapper.selectBuProjectList(buProject);
    }

    /**
     * 新增项目
     * 
     * @param buProject 项目
     * @return 结果
     */
    @Override
    public int insertBuProject(BuProject buProject)
    {
        buProject.setCreateTime(DateUtils.getNowDate());
        return buProjectMapper.insertBuProject(buProject);
    }

    /**
     * 修改项目
     * 
     * @param buProject 项目
     * @return 结果
     */
    @Override
    public int updateBuProject(BuProject buProject)
    {
        buProject.setUpdateTime(DateUtils.getNowDate());
        return buProjectMapper.updateBuProject(buProject);
    }

    /**
     * 批量删除项目
     * 
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteBuProjectByIds(List<Long> ids)
    {
        int count = buDiaryService.countDiaryProjectList(ids);
        if (count > 0){
            throw  new ServiceException("项目下有周报,不可删除",20012);
        }
        return buProjectMapper.deleteBuProjectByIds(ids);
    }

    /**
     * 删除项目信息
     * 
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteBuProjectById(Long id)
    {
        return buProjectMapper.deleteBuProjectById(id);
    }

    @Override
    public int countProjectInProductIds(List<Long> ids) {
        return buProjectMapper.countProjectInProductIds(ids);
    }

    @Override
    public List<BuProject> getProjectList() {
        return buProjectMapper.getProjectList();
    }

    @Override
    public BuProjectEditVO selectProjectByPrimary(Long id) {
        return buProjectMapper.selectProjectByPrimary(id);
    }

    @Transactional
    @Override
    public void addProject(BuProjectAddVO addVO) {
        BuProject  buProject = new BuProject();
        String creatBy = SecurityUtils.getUsername();

        BeanUtils.copyProperties(addVO, buProject);
        buProject.setCreateBy(creatBy);
        buProject.setUpdateBy(creatBy);
        // 插入项目信息
        int primaryKey = this.insertBuProject(buProject);
        List<BuMilestone> milestones = addVO.getMilestones();
        if (!CollectionUtils.isEmpty(milestones)){
            for (BuMilestone milestone : milestones) {
                milestone.setProjectId((long) primaryKey);
                milestone.setCreateBy(creatBy);
                milestone.setUpdateBy(creatBy);
            }
            // 插入里程碑信息
            buMilestoneService.batchInsertMilestone(milestones);
        }
    }

    /**
     * 修改项目
     * //找到被页面删除的里程碑列表，如果有周报关联则不让删除
     *         //否则，删除页面删除的里程碑数据
     *   //有id做更新
     *  //没有id做插入
     * @param editVO 项目
     * @return 结果
     */
    @Transactional
    @Override
    public void updateProject(BuProjectEditVO editVO) {
        //todo 更新项目
        BuProject  buProject = new BuProject();
        String updateBy = SecurityUtils.getUsername();
        buProject.setCreateBy(updateBy);
        BuProjectEditVO buProjectOldVO = this.selectProjectByPrimary(editVO.getId());
        // 原来项目的里程碑信息
        List<BuMilestone> oldMilestones = buProjectOldVO.getMilestones();
        // 修改后的里程碑
        List<BuMilestone> newMilestones = editVO.getMilestones();
        // 待更新
        List<BuMilestone> updateMilesList = new ArrayList<>();
        // 待插入
        List<BuMilestone> insertMilesList = new ArrayList<>();
        List<Long> deletedMileIds = new ArrayList<>();
        if(!CollectionUtils.isEmpty(newMilestones)) {
            insertMilesList = newMilestones.stream().filter(e -> e.getId()==null).collect(Collectors.toList());
            updateMilesList = newMilestones.stream().filter(e -> e.getId()!=null).collect(Collectors.toList());
            List<BuMilestone> deleteMilesList = oldMilestones.stream().filter(buMilestone -> !newMilestones.contains(buMilestone)).collect(Collectors.toList());
            deletedMileIds = deleteMilesList.stream().map(BuMilestone::getId).collect(Collectors.toList());
        }
        if(deletedMileIds.size() != 0) {
            int count = buDiaryMapper.selectByMilestoneIds(deletedMileIds);
            if(count > 0) {
                throw new ServiceException("项目下里程碑有关联周报记录，不允许删除里程碑信息",20013);
            }
            //无任何关联
            buMilestoneService.deleteBuMilestoneByIds(deletedMileIds);
        }
        //有id更新
        if(updateMilesList.size() != 0) {
            for(BuMilestone buMilestone:updateMilesList) {
                buMilestone.setUpdateTime(DateUtils.getNowDate());
                buMilestone.setUpdateBy(updateBy);
                buMilestoneService.updateBuMilestone(buMilestone);
            }
        }
        //无id插入
        if(insertMilesList.size() != 0) {
            for (BuMilestone buMilestone : insertMilesList) {
                buMilestone.setCreateBy(updateBy);
                buMilestone.setUpdateBy(updateBy);
                buMilestone.setProjectId(editVO.getId());
            }
            buMilestoneService.batchInsertMilestone(insertMilesList);
        }
        BeanUtils.copyProperties(editVO,buProject);
        this.updateBuProject(buProject);

    }
}
